package com.wishwall.resources;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.wishwall.beans.DeviceBean;
import com.wishwall.beans.basic.ErrorBean;
import com.wishwall.beans.basic.StatusBean;
import com.wishwall.exceptions.WishWallException;
import com.wishwall.persistence.PersistenceContext;
import com.wishwall.persistence.TransactionalAction;
import com.wishwall.persistence.managers.DeviceManager;
import com.wishwall.utils.ServiceUtils;
import com.wishwall.web.utils.RequestHelper;

@Path("/devices")
public class DeviceResource {

	@javax.ws.rs.core.Context
	javax.servlet.http.HttpServletRequest request;

	@POST
	@Path("/device/register")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response registerDevice(final DeviceBean deviceBean) {

		final PersistenceContext persistenceContext = RequestHelper.getPersistenceContext(request);
		try {

			TransactionalAction action = new TransactionalAction() {

				public void run() {
					DeviceManager deviceManager = persistenceContext.getDeviceManager();
					deviceManager.register(deviceBean, null, null);
				};

			};
			persistenceContext.run(action);

			StatusBean statusBean = ServiceUtils.getStatusBean(deviceBean.getDeviceUid(), null);
			return Response.ok(statusBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(ErrorBean.INTERNAL_ERROR, e);
			return response;
		}
	}

	@GET
	@Path("/device/{deviceId}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getDevice(final @PathParam("deviceId") int deviceId) {

		PersistenceContext persistenceContext = RequestHelper.getPersistenceContext(request);
		try {
			DeviceManager deviceManager = persistenceContext.getDeviceManager();
			DeviceBean deviceBean = deviceManager.getDevice(deviceId);

			return Response.ok(deviceBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(ErrorBean.INTERNAL_ERROR, e);
			return response;
		}
	}

	@GET
	@Path("/device/deviceUid={deviceUid}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getDeviceByUid(final @PathParam("deviceUid") String deviceUid) {

		PersistenceContext persistenceContext = RequestHelper.getPersistenceContext(request);
		try {
			DeviceManager deviceManager = persistenceContext.getDeviceManager();
			DeviceBean deviceBean = deviceManager.getDeviceByUID(deviceUid);

			return Response.ok(deviceBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(ErrorBean.INTERNAL_ERROR, e);
			return response;
		}
	}

	@PUT
	@Path("/device/{deviceId}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response modifyDevice(final @PathParam("deviceId") int deviceId,
			final DeviceBean deviceBean) {

		final PersistenceContext persistenceContext = RequestHelper.getPersistenceContext(request);
		try {

			TransactionalAction action = new TransactionalAction() {

				public void run() throws WishWallException {
					DeviceManager deviceManager = persistenceContext.getDeviceManager();
					deviceManager.modifyDevice(deviceBean, deviceId);
				};

			};
			persistenceContext.run(action);

			StatusBean statusBean = ServiceUtils.getStatusBean(deviceBean.getId(), null, request);
			return Response.ok(statusBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(ErrorBean.INTERNAL_ERROR, e);
			return response;
		}
	}

	@PUT
	@Path("/device/deviceUid={deviceUid}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response modifyDevice(final @PathParam("deviceUid") String deviceUid,
			final DeviceBean deviceBean) {

		final PersistenceContext persistenceContext = RequestHelper.getPersistenceContext(request);
		try {

			TransactionalAction action = new TransactionalAction() {

				public void run() throws WishWallException {
					DeviceManager deviceManager = persistenceContext.getDeviceManager();
					deviceManager.modifyDevice(deviceBean, deviceUid);
				};

			};
			persistenceContext.run(action);

			StatusBean statusBean = ServiceUtils.getStatusBean(deviceBean.getDeviceUid(), null);
			return Response.ok(statusBean).type(MediaType.APPLICATION_JSON).build();
		} catch (Exception e) {
			Response response = ServiceUtils.getErrorResponse(ErrorBean.INTERNAL_ERROR, e);
			return response;
		}
	}

}
